﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../Content/highlighter/styles/shCore.css" />
    <link type="text/css" rel="stylesheet" href="../Content/highlighter/styles/shCoreEclipse.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>Razor模板生成</h3><br/>
    <p>
        控制器实体类<br />
        <pre class="brush:csharp;">
            using NFinal;
            //此代码由NFinalControllerGenerator生成。
            //http://bbs.nfinal.com
            namespace NFinalCoreWebSample.Controllers.IndexController_Model
            {
                public class Default
                {
                    [ViewBagMember]
                    [Newtonsoft.Json.JsonIgnore]
                    public NFinal.Collections.FastSearch.FastSearch&lt;NFinal.StringContainer> systemConfig;
                    public string Message;
		            public string Title;
                }
            }
        </pre>

        <b>Razor源文件</b><br />
        <img src="../Content/images/nfinalcompiler/Razor/Razor源码.jpg" /><br />
        <b>当保存该cshtml文件时自动生成了Default.cshtml文件</b><br/>
        <img src="../Content/images/nfinalcompiler/Razor/Razor目录.jpg"/><br />
        
        <b>生成模板源码：</b>
        <pre class="brush:csharp;">
            using NFinal;

            //此代码由NFinalRazorGenerator生成。
            //http://bbs.nfinal.com
            namespace NFinalCorePlug.Views.Index
            {
                [View("/NFinalCorePlug/Views/Index/Default.cshtml")]
                public class Default : NFinal.View.RazorView&lt;NFinalCorePlug.Controllers.IndexController_Model.Default>
                {
                    public Default(NFinal.IO.Writer writer, NFinalCorePlug.Controllers.IndexController_Model.Default Model) : base(writer, Model)
                    {
                    }
                    //如果此处报错，请添加NFinal引用
                    //PMC命令为：Install-Package NFinal2
                    public override void Execute()
                    {
                        writer.Write("");
                        writer.Write("&lt;!DOCTYPE html>\r\n&lt;html>\r\n\r\n&lt;head>\r\n    &lt;meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n    &lt;script>1&lt;/script>\r\n    &lt;title>");
                        writer.Write(Model.Title);
                        writer.Write("&lt;/title>\r\n\t&lt;meta http-equiv=\"Cache-Control\" content=\"no-cache\">\r\n&lt;/head>\r\n&lt;body>\r\n    &lt;h2>Message:");
                        writer.Write(Model.Message);
                        writer.Write("&lt;/h2>\r\n&lt;/body>\r\n&lt;/html>");
                    }
                }
            }
        </pre>
        其中NFinal.View.RazorView为泛型类，NFinalCoreWebSample.Controllers.IndexController_Model为实体类
        <pre class="brush:csharp;">
            public interface IView
            {
                NFinal.IO.Writer writer { get; set; }
                void Execute();
            }
            public abstract class RazorView&lt;T>:IView
            {
                public bool Render&lt;TModel>(string url, TModel t)
                {

                    NFinal.ViewDelegateData dele;
                    if (NFinal.ViewDelegate.viewFastDic != null)
                    {
                        if (NFinal.ViewDelegate.viewFastDic.TryGetValue(url, out dele))
                        {
                            if (dele.renderMethod == null)
                            {
                                dele.renderMethod = NFinal.ViewDelegate.GetRenderDelegate&lt;TModel>(url,Type.GetTypeFromHandle(dele.viewTypeHandle));
                                NFinal.ViewDelegate.viewFastDic[url] = dele;
                            }
                            var render = (NFinal.RenderMethod&lt;TModel>)dele.renderMethod;
                            render(this.writer, t);
                            return true;
                        }
                        else
                        {
                            //模板未找到异常
                            throw new NFinal.Exceptions.ViewNotFoundException(url);
                        }
                    }
                    else
                    {
                        throw new NFinal.Exceptions.ViewNotFoundException(url);
                    }
                }
              
                public RazorView(NFinal.IO.Writer writer,T Model)
                {
                    this.writer = writer;
                    this.Model = Model;
                }
                public NFinal.IO.Writer writer { get; set; }
                public T Model { get; set; }
                public abstract void Execute();
            }
        </pre>
</p>
</body>
</html>